package team.fhhm.bigwebhomeworkservert.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import team.fhhm.bigwebhomeworkservert.model.Employee;
import team.fhhm.bigwebhomeworkservert.model.Leave;
import team.fhhm.bigwebhomeworkservert.model.LoginInfo;
import team.fhhm.bigwebhomeworkservert.repo.EmployeeRepository;
import team.fhhm.bigwebhomeworkservert.repo.LeaveRepository;

@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api")

public class EmployeeController {

    @Autowired
    EmployeeRepository erepository;
    @Autowired
    LeaveRepository lrepository;

    @GetMapping("/employees")
    public List<Employee> getAllEmployees() {

        List<Employee> employees = new ArrayList<>();
        erepository.findAll().forEach(employees::add);
        return employees;
    }

//    @PostMapping("/employees/create")
//    public boolean postEmployee(@RequestBody Employee employee) {
//        Employee emp = eRepository.findById(employee.getEid()).orElse(null);
//        if (emp == null) {
//        	Object obj = eRepository.save(employee);
//        	return true;
//        } else {
//        	return false;
//        }
//    }


//    @PutMapping("/employees/update")
//    public ResponseEntity<Employee> updateEmloyee(@RequestBody Employee employee) {
//      Optional<Employee> employeeData = erepository.findById(employee.getEid());
//      if (employeeData.isPresent()) {
//          return new ResponseEntity<>(erepository.save(employee), HttpStatus.OK);
//      }
//      else
//          return new ResponseEntity<>(HttpStatus.NOT_FOUND);
//    }
//    	
    	
    @PostMapping("/employees/create")
    public  ResponseEntity<String> createEmployee(@RequestBody Employee employee) {
    	
    	Optional<Employee> employeeData = erepository.findById(employee.getEid());
    	if (employeeData.isPresent() || employee.getEid() == null || employee.getPhone() == null) {
    		return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    	}
    		
    	erepository.save(employee);
    	return new ResponseEntity<>(HttpStatus.OK);
    }

    @DeleteMapping("/employees/delete/{eid}")
    public ResponseEntity<Boolean> deleteEmployee(@PathVariable("eid") String eid) {
    	
    	Optional<Employee> employeeData = erepository.findById(eid);
    	if (employeeData.isPresent()) {
    		Employee employee = employeeData.get();
    		List<Leave> leaves = new ArrayList<Leave>();
            lrepository.findByEmployeeOrderBySubmitDescStarttimeAscEndtimeAsc(employee).forEach(leaves::add);
            for (int i = 0; i < leaves.size(); i ++)
            	lrepository.deleteById(leaves.get(i).getLid());
            erepository.deleteById(eid);
            return new ResponseEntity<>(HttpStatus.OK);
    	}
    	return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    @PutMapping("/employees/update")
    public ResponseEntity<String> updateEmloyee(@RequestBody Employee employee) {
    	
        Optional<Employee> employeeData = erepository.findById(employee.getEid());
        if (employeeData.isPresent()) {
        	erepository.save(employee);
            return new ResponseEntity<>(HttpStatus.OK);
        }
        else
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

    @PostMapping("/login")
    public Employee hasEmployee(@RequestBody LoginInfo loginInfo) {
    	
        Employee employee = erepository.findOneByEidAndPassword(loginInfo.getUsername(),loginInfo.getPassword()).orElse(null);
        if (employee == null)
            return null;
        return employee;
    }
}